class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int left = 1;
        int right = 0;
        for (int i = 0; i < piles.length; i++) {
            right = Math.max(piles[i], right);
        }
        while (left < right) {
            int mid = (left + right) >> 1;
            if (canEatDone(piles, h, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return right;
    }

    private boolean canEatDone(int[] piles, int h, int k) {
        int hours = 0;
        for (int i = 0; i < piles.length; i++) {
            hours = hours + piles[i] / k;
            if (piles[i] % k > 0) hours++;
        }
        return hours <= h;
    }
}
